<?php
/**
 * 对外接口使用的类
 * Enter description here ...
 * @author transn
 *
 */
class ApiAction extends BaseAction{
	/**
	 * 登录验证
	 * Enter description here ...
	 */
	function login(){
		$u_name=$this->in("u_name");
		$u_pwd=$this->in("u_pwd_enc");
		$verify_id=$this->in("verify_id");
		$timestamp=$this->in("timestamp");
		$user=D("User");
		$search=array();
		$search["U_NAME"]=$u_name;
		$data=$user->where($search)->find();
		$IS_USER_OK=false;
		if($data){
			$IS_USER_OK=true;
		}else{
			//echo self::USER_LOGIN_NAME_ERR;
			//exit;
			//检查是否邮箱登录
			$user=D("User");
			$search=array();
			$search["U_EMAIL"]=$u_name;
			$data=$user->where($search)->find();
			if($data){
				$IS_USER_OK=true;
			}else{
				//检查是否手机登录
				$user=D("User");
				$search=array();
				$search["U_MOBILE"]=$u_name;
				$data=$user->where($search)->find();
				if($data){
					$IS_USER_OK=true;
				}
			}
			
		}
		if($IS_USER_OK && $data){
			$pwd=$data["U_PWD"];
			$u_id=$data["U_ID"];
			$u_gid=$data["U_GU_ID"];
			//if($pwd!=md5($u_pwd)){
			//密码已经是MD5的因此不再使用MD5加密
			if($pwd!=$u_pwd){
				$arr=array("result"=>false,"msg"=>"密码错误");
			}else{
				$roles=array('SRVTEAM','WORKER');
				//判断是否存在登录权限
				$_roles=ExUse::getUserRolesByArr($u_id);
				if($_roles && is_array($_roles) && count($_roles)){
					//如果是多角色  先取一个角色使用
					$roleShortname=$_roles[0]["R_SHORT_NAME"];
					if(!in_array($roleShortname, $roles)){
						$arr=array("result"=>false,"msg"=>"无登录权限");
					}else{
						$arr=array("result"=>true,"u_gid"=>$u_gid);
					}
				}else{
					$arr=array("result"=>false,"msg"=>"无登录权限");
				}
			}
			//判断是否存在权限
			echo json_encode($arr);
			exit;
			
		}else{
			$arr=array("result"=>false,"msg"=>"用户错误");
			echo json_encode($arr);
			exit;
		}
		
		exit;
	}
	/**
	 * 得到订单信息
	 * Enter description here ...
	 */
	function getorder(){
		$pageIndex=$this->in("pageindex");//当前页
		$pageSize=$this->in("pagesize");//每页记录条数
		$u_gid=$this->in("u_gid");
		$type=$this->in("type");
		if(!$type){
			$type=1;
		}
		if(!$pageIndex){
			$pageIndex=1;
		}
		if(!$pageSize){
			$pageSize=50;
		}
		$user=D("User");
		$sUser=array();
		$sUser["U_GU_ID"]=$u_gid;
		$dUser=$user->where($sUser)->find();
		$u_id='';
		if($dUser){
			$u_id=$dUser["U_ID"];
		}
		if(!$u_id){
			$arr=array("result"=>false,"msg"=>"无权限访问");
			echo json_encode($arr);
			exit;
		}
		//判断用户的角色
		$_roles=ExUse::getUserRolesByArr($u_id);
		$roleShortname='';
		if($_roles && is_array($_roles) && count($_roles)){
			$roleShortname=$_roles[0]["R_SHORT_NAME"];
		}
		if(!$roleShortname){
			$arr=array("result"=>false,"msg"=>"无权限访问");
			echo json_encode($arr);
			exit;
		}
		$search=array();
		
		if($roleShortname=="WORKER"){
			if($type==1){
				$search["W_ID"]=$u_id;
				$search["O_STATE"]=array('in',array(10,20));
			}else if($type==2){
				$search["O_STATE"]=array('in',array(30));
				$search["W_ID"]=$u_id;
			}else if($type==3){
				$search["O_STATE"]=array('in',array(40,50,60,70,80));
				$search["W_ID"]=$u_id;
			}
			$search["IS_DEL"]=0;
			
		}else{
			$sst=D("Srv_team_members");
			$sst->find($u_id);
			$st_id=$sst->ST_ID;
			if($type==1){
				$search["ST_ID"]=$st_id;
				$search["O_STATE"]=array('in',array(10,20));
			}else if($type==2){
				$search["O_STATE"]=array('in',array(30));
				$search["ST_ID"]=$st_id;
			}else if($type==3){
				$search["O_STATE"]=array('in',array(40,50,60,70,80));
				$search["ST_ID"]=$st_id;
			}
			$search["IS_DEL"]=0;
		}
		if(!count($search)){
			$arr=array("result"=>false,"msg"=>"无权限访问");
			echo json_encode($arr);
			exit;
		}
		$order=D("Service_order");
		//返回总条数
		$order1=D("Service_order");
		$count=$order1->where($search)->count();
		$pi=($pageIndex-1)*$pageSize;
		$data=$order->where($search)->limit($pi.",".$pageSize)->order("CREATED_AT asc")->select();
		
		$out=array();
		if($data && count($data)){
			foreach ($data as $v){
				$tmp=array();
				$tmp["O_ID"]=$v["O_ID"];
				$tmp["O_NO"]=$v["O_NO"];
				$tmp["O_NAME"]=ExUse::getOrderType($v["O_TYPE"]);
				$tmp["type"]=$type;
				$tmp["O_STATE_ID"]=$v["O_STATE"];
				$tmp["O_STATE"]=ExUse::getOrderState($v["O_STATE"]);
				if($v["O_S_TYPE"]){
					$O_S_TYPE=$v["O_S_TYPE"];
				}else{
					$O_S_TYPE=1;
				}
				$tmp["O_S_TYPE"]=getDictValueByKey("INSTALL_MODE",$O_S_TYPE);
				$O_REQ_TIME='';
				if($v["O_REQ_TIME"]){
					$O_REQ_TIME=date("Y-m-d H:i:s",$v["O_REQ_TIME"]);
				}
				$tmp["O_REQ_TIME"]=$O_REQ_TIME;
				$tmp["CU_NAME"]=$v["CU_NAME"]?$v["CU_NAME"]:"";
				$tmp["CU_MOBILE"]=$v["O_MOBILE"]?$v["O_MOBILE"]:"";
				//$AR_ID=$tmpData["AR_ID"];
				$tData=ExUse::getArea($v["AR_ID"]);
				
				$STR=implode(' ',$tData);
				$vname=ExUse::getVTName($v["VT_ID"]);
				if(!$vname){
					$vname='';
				}
				$O_AREA=$STR.' '.$vname;
				$tmp["CU_ADDRESS"]=$O_AREA;
				$tmp["CU_ADDRESS_DETAIL"]=$v["O_ADDRESS"]?$v["O_ADDRESS"]:"";
				$tmp["O_PRODUCT"]=$v["P_BRAND"].' '.$v["P_CATEGORY"].' '.$v["P_MODEL"];
				$tmp["O_PRODUCT_COUNT"]=$v["P_COUNT"]?$v["P_COUNT"]:"";
				$tmp["P_CHANNEL"]=$v["P_CHANNEL"]?$v["P_CHANNEL"]:"";
				$tmp["P_BUY_TIME"]=formatDate($v["P_BUY_TIME"],'Y-m-d');
				$tmp["SO_ID"]=$v["SO_ID"]?$v["SO_ID"]:"";
				$tmp["O_SRV_APPOINT_TIME"]=formatDate($v["O_SRV_APPOINT_TIME"]);
				$tmp["O_SRV_TIME"]=formatDate($v["O_SRV_TIME"]);
				$tmp["O_TIME"]=date("Y-m-d H:i:s",$v["CREATED_AT"]);
				$tmp["O_MEMO"]=$v["O_MEMO"];
				$tmp["O_ST_MEMO_EXT"]=$v["O_ST_MEMO_EXT"];
				$tmp["O_CS_MEMO_EXT"]=$v["O_CS_MEMO_EXT"];
				$tmp["P_BAR_CODE"]=$v["P_BAR_CODE"]?$v["P_BAR_CODE"]:"";
				$img_url='';
				if($v["P_INSTALL_CARD"]){
					$http_icard=strtolower($v["P_INSTALL_CARD"]);
					if(strstr($http_icard, "http://")){
						$img_url = $v["P_INSTALL_CARD"];
					}else{
						$img_url = "http://".$_SERVER["HTTP_HOST"]."/".$v["P_INSTALL_CARD"];
					}
				}
				$tmp["P_INSTALL_CARD"]=$img_url;
				
				$out[]=$tmp;
			}
		}
		$rev=array("result"=>true,"pageIndex"=>$pageIndex,"recordCount"=>$count,"items"=>$out);
		echo json_encode($rev);
		exit;
		
	}
	/**
	 * 完成接口
	 * Enter description here ...
	 */
	function finishorder(){
		$o_id=$this->in("o_id");
		$u_id=$this->in("u_gid");
		$p_bar_code=$this->in("p_bar_code");
		$o_srv_time=$this->in("o_srv_time");
		$o_stuff_details=$this->in("o_stuff_details");
		$o_cost=$this->in("o_cost");
		$o_st_memo=$this->in("o_st_memo");
		$xy=$this->in("xy");
		$verify_id=$this->in("verify_id");
		$timestamp=$this->in("timestamp");
		$o_card_url=$this->in("o_card");
		$user=D("User");
		$userSearch=array();
		$userSearch["U_GU_ID"]=$u_id;
		$userData=$user->where($userSearch)->find();
		$_uid="";
		if($userData){
			$_uid=$userData["U_ID"];
		}
		$order=D("Service_order");
		$order->find($o_id);
		$order->P_INSTALL_CARD=$o_card_url?$o_card_url:"";
		$order->P_BAR_CODE=$p_bar_code;
		$order->O_SRV_TIME=$o_srv_time?strtotime($o_srv_time):time();
		$order->O_ST_MEMO=$o_st_memo;
		$order->O_STUFF_DETAILS=$o_stuff_details;
		$order->O_COST=$o_cost;
		$order->O_INSTALL_USER=$_uid;
		$order->O_STATE=40;
			
		$logOid=$order->O_ID;
		$logONo=$order->O_NO;
		$rev=false;
		if($order->save()){
			if($xy){
				$map=D('Map');
				$mData["O_ID"]=$o_id;
				$mData["U_ID"]=$_uid;
				$mData["M_XY"]=$xy;
				$mData["CREATED_AT"]=time();
				$mData["UPDATED_AT"]=time();
				$map->add($mData);
			}
			
			$rev=true;
			$msg=getUserName($u_id)."在".formatDate(time())."处理服务单。改变状态为:".ExUse::getOrderState(40);
   			ExLog::addLog($msg, 'TYPE_ORDER_CHANGE_STATE',2,$logOid,$logONo);
		}
		if($rev){
			$arr=array("result"=>true);
		}else{
			$arr=array("result"=>false,"msg"=>"数据保存失败");
		}
		echo json_encode($arr);
		exit;
	}
	function finish(){
		$o_id=$this->in("o_id");
		$u_id=$this->in("u_gid");
		$p_bar_code=$this->in("p_bar_code");
		$o_srv_time=$this->in("o_srv_time");
		$o_stuff_details=$this->in("o_stuff_details");
		$o_cost=$this->in("o_cost");
		$o_st_memo=$this->in("o_st_memo");
		$xy=$this->in("xy");
		$verify_id=$this->in("verify_id");
		$timestamp=$this->in("timestamp");
		$o_card_url=$this->in("o_file_url");
		$user=D("User");
		$userSearch=array();
		$userSearch["U_GU_ID"]=$u_id;
		$userData=$user->where($userSearch)->find();
		$_uid="";
		if($userData){
			$_uid=$userData["U_ID"];
		}
		$order=D("Service_order");
		$order->find($o_id);
		$order->P_INSTALL_CARD=$o_card_url?$o_card_url:"";
		$order->P_BAR_CODE=$p_bar_code;
		$order->O_SRV_TIME=$o_srv_time?strtotime($o_srv_time):time();
		$order->O_ST_MEMO=$o_st_memo;
		$order->O_STUFF_DETAILS=$o_stuff_details;
		$order->O_COST=$o_cost;
		$order->O_INSTALL_USER=$_uid;
		$order->O_STATE=40;
			
		$logOid=$order->O_ID;
		$logONo=$order->O_NO;
		$rev=false;
		if($order->save()){
			if($xy){
				$map=D('Map');
				$mData["O_ID"]=$o_id;
				$mData["U_ID"]=$_uid;
				$mData["M_XY"]=$xy;
				$mData["CREATED_AT"]=time();
				$mData["UPDATED_AT"]=time();
				$map->add($mData);
			}
			
			$rev=true;
			$msg=getUserName($u_id)."在".formatDate(time())."处理服务单。改变状态为:".ExUse::getOrderState(40);
   			ExLog::addLog($msg, 'TYPE_ORDER_CHANGE_STATE',2,$logOid,$logONo);
		}
		if($rev){
			$arr=array("result"=>true);
		}else{
			$arr=array("result"=>false,"msg"=>"数据保存失败");
		}
		echo json_encode($arr);
		exit;
	}
	/**
	 * 设置当前用户的位置信息
	 * Enter description here ...
	 */
	function setxy(){
		$u_gid=$this->in("u_gid");
		$xy=$this->in("xy");
		$o_explain=$this->in("o_explain");
		$verify_id=$this->in("verify_id");
		$timestamp=$this->in("timestamp");
		$user=D("User");
		$userSearch=array();
		$userSearch["U_GU_ID"]=$u_gid;
		$userData=$user->where($userSearch)->find();
		$_uid="";
		if($userData){
			$_uid=$userData["U_ID"];
		}
		$map=D('Map');
		//$mData["O_ID"]=$o_id;
		$mData["U_ID"]=$_uid;
		$mData["M_XY"]=$xy;
		$mData["M_EXPLAIN"]=$o_explain;
		$mData["CREATED_AT"]=time();
		$mData["UPDATED_AT"]=time();
		$rev=$map->add($mData);
		if($rev){
			$arr=array("result"=>true);
		}else{
			$arr=array("result"=>false,"msg"=>"数据保存失败");
		}
		echo json_encode($arr);
		exit;
	}
	/**
	 * 
	 * Enter description here ...
	 */
	function getUploadUrl(){
		$o_id=$this->in("o_id");
		$__shijian=date("Ym",time());
		$up_url=C("SYS_UPLOAD_CARD_DIR").$__shijian;
		if(is_dir($up_url)){
			
		}else{
			mkdir($up_url,0777);
			
		}
		$order=D("Service_order");
		$order->find($O_ID);
		$o_no=$order->O_NO;
		if(!$o_no){
			$o_no=getOrderNumber();
		}
		$up_url.="/".$o_id;
		if(is_dir($up_url)){
			
		}else{
			mkdir($up_url,0777);
			
		}
		echo json_encode(array("url"=>$up_url,"o_no"=>$o_no));
		exit;
	}
	/**
	 * 提交订单那数据到总部
	 * Enter description here ...
	 */
	function setHQData(){
		set_time_limit(0);
		$order=M();
		$sql="select * from as_service_order where O_STATE=50 and IS_SUBMIT_HQ=0 and IS_DEL=0";
		$data=$order->query($sql);
		$revData=array();
		$i=0;
		if(is_array($data) && count($data)){
			foreach ($data as $val){
				$o_no=$val["O_NO"];
				//旧数据不处理
				if(strlen($o_no)!=12){
					continue;
				}
				$revData[$i]["ProofNo"]=$o_no;
				$revData[$i]["SellKind"]="1";
				$revData[$i]["SellUnit"]=$val["P_CHANNEL"]?$val["P_CHANNEL"]:"";
				$revData[$i]["BillJe"]=$val["O_INVOICE_PRICE"]?$val["O_INVOICE_PRICE"]:"";//存在问题
				$revData[$i]["WxJe"]=$val["O_NS_SUBSIDY"]?$val["O_NS_SUBSIDY"]:"";
				$revData[$i]["BuyDate"]=$val["P_BUY_TIME"]?date("Y-n-d",$val["P_BUY_TIME"]):"";
				$revData[$i]["GazetteActDate"]=$val["CREATED_AT"]?date("Y-n-d",$val["CREATED_AT"]):"";
				$revData[$i]["FitDate"]=$val["O_SRV_TIME"]?date("Y-n-d",$val["O_SRV_TIME"]):"";
				$revData[$i]["InBarCode"]=$val["P_BAR_CODE"]?$val["P_BAR_CODE"]:"";
				$revData[$i]["KindCode"]=getProductNo($val["P_CATEGORY"]);
				$revData[$i]["TypeCode"]=getProductNo($val["P_MODEL"],2);;
				$revData[$i]["yName"]=$val["CU_NAME"]?$val["CU_NAME"]:"";
				$revData[$i]["yTel"]=$val["O_MOBILE"]?$val["O_MOBILE"]:"";
				$revData[$i]["yTel1"]=$val["O_PHONE"]?$val["O_PHONE"]:"";
				$revData[$i]["yAddress"]=$val["O_ADDRESS"]?$val["O_ADDRESS"]:"";
				$revData[$i]["yPostCode"]=$val["O_POSTCODE"]?$val["O_POSTCODE"]:"";
				$srv_team=D("Srv_team");
				$teams=array();
				$teams["ST_ID"]=$val["ST_ID"];
				$teamData=$srv_team->where($teams)->field("ST_NAME,ST_PHONE,O_CENTER_CODE,O_STATION_CODE")->find();
				$O_CENTER_CODE='';
				$O_STATION_CODE='';
				$ST_NAME='';
				$ST_PHONE='';
				if($teamData){
					$O_CENTER_CODE=$teamData["O_CENTER_CODE"];
					$O_STATION_CODE=$teamData["O_STATION_CODE"];
					$ST_NAME=$teamData["ST_NAME"];
					$ST_PHONE=$teamData["ST_PHONE"];
				}
				$revData[$i]["cCodeNo"]=$O_CENTER_CODE;
				$revData[$i]["pCodeNo"]=$O_STATION_CODE;
				
				$revData[$i]["ServiceDeptName"]=$ST_NAME;
				$revData[$i]["ServicePersName"]=$val["O_INSTALL_USER"]?$val["O_INSTALL_USER"]:"";
				$revData[$i]["pTel"]=$ST_PHONE;
				$revData[$i]["FitCost"]=$val["O_INST_FEE"]?$val["O_INST_FEE"]:"0";
				$revData[$i]["TravelCost"]=$val["O_TRAVEL_EXPENSE"]?$val["O_TRAVEL_EXPENSE"]:"0";
				$revData[$i]["GadgetCost"]=$val["O_MATERIAL_FEE"]?$val["O_MATERIAL_FEE"]:"0";
				$revData[$i]["OtherCost"]=$val["O_OTHER_COST"]?$val["O_OTHER_COST"]:"0";
				$revData[$i]["BzAccnt"]=$val["O_MEMO"]?$val["O_MEMO"]:"";
				$revData[$i]["SwitchIf"]=$val["O_INSTOPT_LOUDIANKAIGUAN"]?$val["O_INSTOPT_LOUDIANKAIGUAN"]:"0";
				$revData[$i]["GNDIf"]=$val["O_INSTOPT_LIANGHAOJIEDI"]?$val["O_INSTOPT_LIANGHAOJIEDI"]:"0";
				$revData[$i]["SafetyIf"]=$val["O_INSTOPT_GAOZHISHIXIANG"]?$val["O_INSTOPT_GAOZHISHIXIANG"]:"0";
				$revData[$i]["BathOutIf"]=$val["O_INSTOPT_YUSHIWAI"]?$val["O_INSTOPT_YUSHIWAI"]:"0";
				$revData[$i]["FumePipeIf"]=$val["O_INSTOPT_PAIYANGUAN"]?$val["O_INSTOPT_PAIYANGUAN"]:"0";
				$revData[$i]["FitinessIf"]=$val["O_INSTOPT_KONGQILIUTONG"]?$val["O_INSTOPT_KONGQILIUTONG"]:"0";
				$revData[$i]["ScRq"]=$val["P_BAR_CODE"]?"20".substr($val["P_BAR_CODE"], 18, 6):"";

				$O_MATERIAL_DETAIL=$val["O_STUFF_DETAILS"]?$val["O_STUFF_DETAILS"]:"";
				$revData[$i]["O_MATERIAL_DETAIL"]=$O_MATERIAL_DETAIL;
				$i++;
			}
		}
		$jsonData=json_encode($revData);
		$param=array("jsondata"=>$jsonData);
		$url=C("HQ_URL");
		$rev=curlPost($url,$param);
		//Log::write($rev, Log::INFO);
		if($rev=="n"){
		}else{
		
		$rev=json_decode($rev,true);
		if($rev && is_array($rev)){
			foreach ($rev as $v){
				if(!$v["result"]){
					continue;
				}
				$sorder=M();
				$sql="update as_service_order set IS_SUBMIT_HQ=1 where O_NO='".$v["O_NO"]."'";
				$sorder->query($sql);
			}
		}
		}
		exit;
		
	}
}